﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ecdl.demo.model;

namespace ecdl.demo.ui.ui.test {
    /// <summary>
    /// Interaction logic for TestEvaluation.xaml
    /// </summary>
    public partial class TestEvaluation : Window, ITestEvaluationPresenter {
        private const double MIN_SUCCESS_PERCENTAGE = 0.75;

        public TestEvaluation() {
            InitializeComponent();
        }

        public void ShowTestEvaluation(IList<TaskEvaluation> evaluations) {
            //update the score labels
            int currentPoints;
            int maxPoints;
            calculatePoints(evaluations, out currentPoints, out maxPoints);
            updateScoreLabel(currentPoints, maxPoints);

            double percent = ((double)currentPoints) / maxPoints;
            updateScoreBar(percent);
            updateSuccessLabel(percent);

            updateEvaluations(evaluations);

            ShowDialog();
        }

        private void updateEvaluations(IList<TaskEvaluation> evaluations) {
            this.taskEvaluationsTree.ItemsSource = evaluations;
        }

        private void updateSuccessLabel(double percent) {
            if (percent >= MIN_SUCCESS_PERCENTAGE) {
                SuccessLabel.Content = "Glückwunsch, Sie haben die Prüfung bestanden!"; 
            } else {
                SuccessLabel.Content = "Leider haben Sie die Prüfung NICHT bestanden!";
                SuccessLabel.Foreground = Brushes.Red;
            }
        }


        private void updateScoreBar(double percent) {
            ScoreBar.Value = percent;
        }

        private void updateScoreLabel(int currentPoints, int maxPoints) {
            string text = String.Format(" {0} / {1} Punkte", currentPoints, maxPoints);
            ScoreLabel.Content = text;
        }

        private void calculatePoints(IList<TaskEvaluation> evaluations, out int currentPoints, out int maxPoints) {
            currentPoints = 0;
            maxPoints = 0;
            foreach (var e in evaluations) {
                currentPoints += e.Points;
                maxPoints += e.Task.MaxPoints;
            }
        }
    }

    public class EvaluationViewModel {
        public EvaluationViewModel(IList<ITaskEvaluation> evaluations) {
            this.Evaluations = evaluations;
        }

        public IList<ITaskEvaluation> Evaluations { get; set; }

    }
}
